package zhihui.mobile.presenter.myself;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zgm.utils.SharedPreferencesUtis;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.order.ShopOrder;
import zhihui.mobile.bean.shop.sales.CouponInfo;
import zhihui.mobile.bean.user.UserInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.CouponManager;
import zhihui.mobile.manager.shop.ShopAttentionManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.express.ExpressActivity;
import zhihui.mobile.ui.myself.IMyselfMainContract;
import zhihui.mobile.ui.shop.attention.ShopAttentionActivity;
import zhihui.mobile.ui.shop.coupon.CouponActivity;
import zhihui.mobile.ui.shop.order.ShopOrderManagerActivity;

/**
 * Created by 张公铭 on 2017/9/11 0011.
 */

public class MyselfMainPresenter implements IMyselfMainContract.IMyselfMainPresenter
{
    private Context mContext;
    private IMyselfMainContract.IMyselfMainView myselfMainView;


    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        myselfMainView = (IMyselfMainContract.IMyselfMainView) view;
    }

    @Override
    public void onResume()
    {
        myselfMainView.refreshViewData(UserManager.getInstance().getCurUser());
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void goOrderByState(final int state)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }

        String params = "0";
        switch (state)
        {
            case 0:
                params = "0";
                break;
            case 1:
                params = "1";
                break;
            case 3:
                params = "2,3,6";
                break;
            case 4:
                params = "4,7";
                break;
            case 5:
                params = "5";
        }

        myselfMainView.showLoading();
        ShopOrderManager.getInstance().getShopOrders(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                myselfMainView.hindLoading();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    int timeover = data.getInt("timeover");
                    ShopOrderManager.getInstance().timeover = timeover;
                    List<ShopOrder> list = new ArrayList<>();
                    Gson gson = new Gson();
                    list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<ShopOrder>>()
                    {
                    }.getType());
                    ShopOrderManager.getInstance().setOrderList(list);

                    Intent intent = new Intent(mContext, ShopOrderManagerActivity.class);
                    intent.putExtra("orderState", state);
                    mContext.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    myselfMainView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                myselfMainView.hindLoading();
                myselfMainView.showMessage(message);
            }
        }, params);
    }

    @Override
    public void goMySales()
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
        myselfMainView.showLoading();
        CouponManager.getInstance().getUseCouponList(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                myselfMainView.hindLoading();
                Gson gson = new Gson();
                try
                {
                    List<CouponInfo> list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<CouponInfo>>()
                    {
                    }.getType());
                    CouponManager.getInstance().setmList(list);
                    Intent intent = new Intent(mContext, CouponActivity.class);
                    mContext.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    myselfMainView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                myselfMainView.hindLoading();
                myselfMainView.showMessage(message);
            }
        }, "", 0, 5101);
    }

    @Override
    public void goPackageService()
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
        mContext.startActivity(new Intent().setClass(mContext, ExpressActivity.class));
    }

    @Override
    public void goMyService()
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
    }

    @Override
    public void goAttention()
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
        myselfMainView.showLoading();
        ShopAttentionManager.getInstance().getMyAttention(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                myselfMainView.hindLoading();
                Gson gson = new Gson();
                List<SpuInfo> list = null;
                try
                {
                    list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    ShopAttentionManager.getInstance().setList(list);

                    Intent intent = new Intent(mContext, ShopAttentionActivity.class);
                    mContext.startActivity(intent);
                } catch (JSONException e)
                {
                    myselfMainView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                myselfMainView.hindLoading();
                myselfMainView.showMessage(message);
            }
        });

    }
}
